﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAD_SiS.Models;
using System.IO;

namespace SAD_SiS.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/


        public ActionResult CustomerCP()
        {
            string _username = getUser();
            if (_username.Equals("guest"))
            {
                ViewBag.Mess = "Bạn phải đăng nhập trước";
                return View("Error");
            }
            using (SADEntities dbContext = new SADEntities())
            {
                var CusInfo = (from customer in dbContext.Customers.Include("Account") where customer.Username == _username select customer).SingleOrDefault();
                if (CusInfo == null)
                {
                    ViewBag.Mess = "Username không tồn tại";
                    return View("Error");
                }
                return View(CusInfo);
            }
        }

        // get
        public ActionResult UpdateInfo()
        {
            string _username = getUser();
            if (_username.Equals("guest"))
            {
                ViewBag.Mess = "Bạn phải đăng nhập trước";
                return View("Error");
            }
            using (SADEntities dbContext = new SADEntities())
            {
                var CusInfo = (from customer in dbContext.Customers.Include("Account") where customer.Username == _username select customer).SingleOrDefault();
                if (CusInfo == null)
                {
                    return View("Error");
                }
                return View(CusInfo);
            }
        }

        [HttpPost]
        public ActionResult UpdateInfo(FormCollection form)
        {
            string _username = getUser();
            string fullname = form["fullname"];
            string phone = form["phone"];
            string street = form["street"];
            string district = form["district"];
            string birthday = form["birthday"];

            using (SADEntities dbContext = new SADEntities())
            {
                var cusInfor = (from customer in dbContext.Customers.Include("Account") where customer.Username == _username select customer).Single();
                //cusInfor.Account.Email = "";
                cusInfor.FullName = fullname;
                cusInfor.Phone = phone;
                cusInfor.Street = street;
                cusInfor.District = district;
                cusInfor.BirthDate = DateTime.Parse(birthday);

                dbContext.SaveChanges();

            }
            return RedirectToAction("CustomerCP");
        }

        public ActionResult ChangePassword()
        {
            string _username = getUser();
            if (_username.Equals("guest"))
            {
                ViewBag.Mess = "Bạn phải đăng nhập trước";
                return View("Error");
            }
            using (SADEntities dbContext = new SADEntities())
            {
                var CusInfo = (from account in dbContext.Accounts where account.Username == _username select account).SingleOrDefault();
                if (CusInfo == null)
                {
                    ViewBag.Mess = "Bạn phải đăng nhập trước";
                    return View("Error");
                }
                return View(CusInfo);
            }
        }

        [HttpPost]
        public ActionResult ChangePassword(FormCollection form)
        {
            string curPass = form["password"];
            string newPass1 = form["newPasswd1"];
            string newPass2 = form["newPasswd2"];
            string _username = getUser();

            using (SADEntities dbContext = new SADEntities())
            {
                var AccInfo = (from account in dbContext.Accounts where account.Username == _username select account).Single();
                if (curPass == AccInfo.Userpass)
                {
                    AccInfo.Userpass = newPass1;
                    dbContext.SaveChanges();
                    return RedirectToAction("CustomerCP");
                }
                else
                {
                    ModelState.AddModelError("", "Sai mật khẩu hiện tại.");
                    ViewBag.messafe = "Wrong password.";
                    return RedirectToAction("ChangePassword");
                }

                //return RedirectToAction("CustomerCP");
            }

        }

        public ActionResult ChangeAvatar()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangeAvatar(HttpPostedFileBase file)
        {
            var avatar = Path.GetFileName(file.FileName);
            string _username = getUser();

            var path = Path.Combine(Server.MapPath("~/Content/images/"), avatar);
            file.SaveAs(path);
            using (SADEntities dbContext = new SADEntities())
            {
                var cus = (from customer in dbContext.Customers where customer.Username == _username select customer).Single();
                cus.Avatar = avatar;
                dbContext.SaveChanges();
            }

            return RedirectToAction("CustomerCP");
        }
        
        private string getUser()
        {
            if (Session["username"] != null)
            {
                return Session["username"].ToString();
            }
            else
            {

                //HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                //if (authCookie == null)
                //{
                //    return "guest";
                //}
                //FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
                //return ticket.Name == null ? "guest" : ticket.Name;
                return "guest";
            }
        }
    }
}
